﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///XCXK_SS 的摘要说明
/// </summary>
public class ZXGZTJ_SS : IPaginationList, ExSystem.ExData.ExITableCellFormat
{

    HttpRequest Request;
    HttpSessionState Session;
    // int totalCount;
    public ZXGZTJ_SS(HttpRequest Request, HttpSessionState Session)
    {

        this.Request = Request;
        this.Session = Session;
        //totalCount = 0;

    }

    private static DataTable CreateDataTable(DataRow[] rows)
    {
        DataTable dt = null;
        if (rows.Length > 0)
        {

            dt = rows[0].Table.Clone();
            foreach (DataRow row in rows)
            {
                DataRow rowx = dt.NewRow();
                rowx.ItemArray = row.ItemArray;
                dt.Rows.Add(rowx);
            }
        }

        return dt;
    }
    #region ExITableCellFormat 成员

    object ExSystem.ExData.ExITableCellFormat.Format(object cellobj)
    {
        if (cellobj==null)return cellobj;
        DateTime d = DateTime.MinValue;
        if(!DateTime.TryParse(cellobj.ToString(),out d))
        {
            return cellobj;
        }
       
        return d.ToString("yyyy-MM-dd");
    }

    #endregion
    #region IPaginationList 成员

    DataTable IPaginationList.GetPaginationData()
    {


        IPaginationList dd = this as IPaginationList;
        DataTable dt = dd.GetTotalData();
        DataTable dt2 = new DataTable();
        int count = dt.Rows.Count;
        #region//排序

        string sortName = Request["sort"];
        string order = Request["order"];
        if (string.IsNullOrEmpty(sortName)) sortName = "ajbh";
        if (string.IsNullOrEmpty(order)) order = "desc";
        if (sortName.Trim().ToLower() == "ajbh") sortName = string.Format(" ahyear {0},ahzt {0},ahz {0},ahh_int ", order);

        DataView dv = dt.DefaultView;
        try
        {
            dv.Sort = string.Format("{0} {1}", sortName, order);
        }
        catch (Exception ex) { }
        dt2 = dv.ToTable();

        #endregion

        #region//分页
        string page = Request["page"];
        string rows = Request["rows"];
        if (string.IsNullOrEmpty(page)) page = "1";
        if (string.IsNullOrEmpty(rows)) rows = "10";
        int page_int = int.Parse(page);
        int rows_int = int.Parse(rows);

        int temp_rows_int = rows_int;
        int aaa = count - (page_int * rows_int);
        if (aaa < 0) temp_rows_int = rows_int + aaa;
        DataRow[] dataRows = new DataRow[temp_rows_int];
        for (int j = 0; j < temp_rows_int; j++)
        {

            dataRows[j] = dt2.Rows[(page_int - 1) * rows_int + j];
        }
        dt2 = CreateDataTable(dataRows);
        if (dt2 == null) dt2 = dt;
        #endregion
        dt2 = ExSystem.ExData.ExDataTable.ToNewTypeColumn(dt2, typeof(string), null, this);
        return dt2;

    }

    int IPaginationList.GetTotalCount()
    {
        IPaginationList dd = this as IPaginationList;
        DataTable dt = dd.GetTotalData();
        return dt.Rows.Count;
    }

    DataTable IPaginationList.GetTotalData()
    {
        int rowindex = 0;
        string fydm;
        string fymc, tsmc, rymc, ksrq, jsrq, lxh, WebPageName;
        DataTable dt = new DataTable();

        if (Request.QueryString["fydm"] != null)
        {
            WebPageName = Request.QueryString["WebPageName"];
            fydm = Request.QueryString["fydm"];
            fymc = HttpUtility.HtmlDecode(Request.QueryString["fymc"]);
            rymc = HttpUtility.HtmlDecode(Request.QueryString["rymc"]);
            tsmc = HttpUtility.HtmlDecode(Request.QueryString["tsmc"]);
            ksrq = Request.QueryString["ksrq"];
            jsrq = Request.QueryString["jsrq"];
            lxh = Request.QueryString["lxh"];
          

            #region//缓存表
            string paras = fymc + rymc + ksrq + jsrq + lxh + fydm + WebPageName;
            if (Session["GetZXGZTJ_FC_Pro_DT_Paras"] != paras)
            {

                dt = BLL.SJA_Service.GetZXGZTJ_FC_Pro(WebPageName, fydm, tsmc, rymc, ksrq, jsrq, lxh);
                dt.Columns.Add("ahh_int", System.Type.GetType("System.Int32"));
                dt.Columns["ahh_int"].Expression = "Convert(ahh, 'System.Int32')";
                Session.Add("GetZXGZTJ_FC_Pro", dt);
                Session.Add("GetZXGZTJ_FC_Pro_DT_Paras", paras);
            }
            else
            {

                dt = Session["GetZXGZTJ_FC_Pro"] as DataTable;
            }
            #endregion

        }
        return dt;
    }
    #endregion
}

